package com.tianji.aigc.config;

import com.tianji.aigc.advisor.RecordOptimizationAdvisor;
import com.tianji.aigc.memory.MysqlChatMemory;
import com.tianji.aigc.tools.CourseTools;
import com.tianji.aigc.tools.OrderTools;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.client.advisor.api.Advisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SpringAIConfig {

    /**
     * 阿里 chatclinet 配置 ChatClient
     */
    @Bean
    public ChatClient dashScopeChatClient(ChatClient.Builder dashScopeChatClientBuilder,
                                 // 日志增强
                                 Advisor loggerAdvisor,
                                 // 会话存储增强
                                 Advisor messageChatMemoryAdvisor,
                                 // 课程Tools
                                 CourseTools courseTools,
                                 // 预下单Tools
                                 OrderTools orderTools,
                                 // 优化对话历史记录 Advisor
                                 Advisor recordOptimizationAdvisor
    ) {
        return dashScopeChatClientBuilder
                //添加 Advisor 功能增强
                .defaultAdvisors(loggerAdvisor, messageChatMemoryAdvisor, recordOptimizationAdvisor)
                // 添加 Tools，实现与业务系统集成
                // .defaultTools(courseTools, orderTools)
                .build();
    }

    /**
     * openai chatclinet 配置 ChatClient（Moonshot AI）
     */
    @Bean
    public ChatClient openAiChatClient(ChatClient.Builder openAiChatClientBuilder,
                                       Advisor loggerAdvisor  // 日志记录器
    ) {
        return openAiChatClientBuilder
                .defaultAdvisors(loggerAdvisor)
                .build();
    }

    /**
     * 日志记录器
     */
    @Bean
    public Advisor loggerAdvisor() {
        return new SimpleLoggerAdvisor();
    }

    // @Bean
    // public ChatMemory redisChatMemory(){
    //     return new RedisChatMemory();
    // }
    //
    // @Bean
    // public Advisor messageChatMemoryAdvisor(ChatMemory redisChatMemory){
    //     return new MessageChatMemoryAdvisor(redisChatMemory);
    // }

    @Bean
    public ChatMemory mysqlChatMemory(){
        return new MysqlChatMemory();
    }

    @Bean
    public Advisor messageChatMemoryAdvisor(ChatMemory mysqlChatMemory){
        return new MessageChatMemoryAdvisor(mysqlChatMemory);
    }

    /**
     * 优化对话历史记录
     */
    @Bean
    public Advisor recordOptimizationAdvisor(MysqlChatMemory mysqlChatMemory) {
        return new RecordOptimizationAdvisor(mysqlChatMemory);
    }

    // @Bean
    // public DashScopeAudioTranscriptionModel dashScopeAudioTranscriptionModel() {
    //     return new DashScopeAudioTranscriptionModel();
    // }


}
